<html>
<head><meta charset="utf-8"><title>Semantics of `a += b` for non-primitive types · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html">Semantics of `a += b` for non-primitive types</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217833108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833108">(Nov 25 2020 at 00:45)</a>:</h4>
<p>Has there been an official decision on the semantics for <code>a += b</code> where neither <code>a</code> nor <code>b</code> are primitives?</p>
<p>Is it just desugar to <code>AddAssign&lt;A&gt;::add_assign(&amp;mut a, b)</code>?</p>



<a name="217833171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833171">(Nov 25 2020 at 00:46)</a>:</h4>
<p>it better, because that's what it says here <a href="https://doc.rust-lang.org/core/ops/trait.AddAssign.html">https://doc.rust-lang.org/core/ops/trait.AddAssign.html</a></p>



<a name="217833431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833431">(Nov 25 2020 at 00:51)</a>:</h4>
<p>What it says there isn't entirely true since primitive types don't actually desugar to the function call (and that should probably be stated such on the trait docs).</p>



<a name="217833491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833491">(Nov 25 2020 at 00:52)</a>:</h4>
<p>And it's actually observable in practice because evaluation order changes.</p>



<a name="217833504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833504">(Nov 25 2020 at 00:52)</a>:</h4>
<p>an observable optimization? in MY rust!?</p>



<a name="217833535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833535">(Nov 25 2020 at 00:53)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/src/test/ui/expr/compound-assignment/eval-order.rs">https://github.com/rust-lang/rust/blob/master/src/test/ui/expr/compound-assignment/eval-order.rs</a> for those curious</p>



<a name="217833756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833756">(Nov 25 2020 at 00:56)</a>:</h4>
<p>oh so, if i follow, <code>primA += primB</code> is eval(primB) then eval(primA), whereas <code>compoundA += compoundB</code> would be eval(compoundA) then eval(compoundB)?</p>



<a name="217833797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/217833797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#217833797">(Nov 25 2020 at 00:57)</a>:</h4>
<p>Yes.</p>



<a name="218119684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218119684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218119684">(Nov 27 2020 at 18:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/217833504">said</a>:</p>
<blockquote>
<p>an observable optimization? in MY rust!?</p>
</blockquote>
<p>worse, not an optimization... just an observable difference in things that should behave the same...</p>



<a name="218121336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218121336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218121336">(Nov 27 2020 at 19:05)</a>:</h4>
<p>a reason for rust 2.0 <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="218122546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218122546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218122546">(Nov 27 2020 at 19:22)</a>:</h4>
<p>I opened an issue for a Clippy lint for the problematic case, but never got around to implementing it: <a href="https://github.com/rust-lang/rust-clippy/issues/4756">https://github.com/rust-lang/rust-clippy/issues/4756</a></p>



<a name="218156808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218156808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218156808">(Nov 28 2020 at 09:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218121336">said</a>:</p>
<blockquote>
<p>a reason for rust 2.0 <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>
</blockquote>
<p>was there an attempt to consolidate this, or is changing evaluation order now considered to be too risky?</p>



<a name="218159525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218159525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218159525">(Nov 28 2020 at 11:08)</a>:</h4>
<p>not sure, I would love to fix this in this version</p>



<a name="218159529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218159529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218159529">(Nov 28 2020 at 11:08)</a>:</h4>
<p>but I kind of fear that there is code which depends on the current order for both cases</p>



<a name="218179001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218179001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218179001">(Nov 28 2020 at 20:08)</a>:</h4>
<p>Is this the kind of thing that could be changed at an edition boundary?</p>



<a name="218179460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218179460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218179460">(Nov 28 2020 at 20:22)</a>:</h4>
<p>Only extremely carefully since we're not changing what people write. We'd have to make the current behavior error when it has an actual side effect and a suggestion on how to rewrite it that doesn't error. Probably not worth the churn TBH.</p>



<a name="218183445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218183445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218183445">(Nov 28 2020 at 22:07)</a>:</h4>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218179460">said</a>:</p>
<blockquote>
<p>We'd have to make the current behavior error when it has an actual side effect and a suggestion on how to rewrite it that doesn't error.</p>
</blockquote>
<p>I think this is the hard part, yeah.  We'd need to be able to write a tolerably-noisy warning that the semantics would change next edition to put in the preceding edition.</p>



<a name="218196592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218196592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218196592">(Nov 29 2020 at 05:06)</a>:</h4>
<p>Anyways, my main question remains unanswered. And it's blocking me on the reference PR for evaluation order.</p>



<a name="218214501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218214501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218214501">(Nov 29 2020 at 14:11)</a>:</h4>
<p>I feel like doing this on an edition boundary would be worse than doing it everywhere... it'd mean the order depends on the Cargo.toml file!</p>



<a name="218214547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218214547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218214547">(Nov 29 2020 at 14:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218196592">said</a>:</p>
<blockquote>
<p>Anyways, my main question remains unanswered. And it's blocking me on the reference PR for evaluation order.</p>
</blockquote>
<p>I thought <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/217833756">this is the answer</a>? We should probably add tests to confirm this.</p>



<a name="218214815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218214815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218214815">(Nov 29 2020 at 14:21)</a>:</h4>
<p>While I think the inconsistency is terrible, is it clear to everyone which order is better? It seems like there are arguments for both choices</p>



<a name="218215793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218215793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218215793">(Nov 29 2020 at 14:50)</a>:</h4>
<p>i think <code>b</code> then <code>a</code> makes more sense but i personally believe it really doesn't matter as long as we are consistent</p>



<a name="218217297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218217297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218217297">(Nov 29 2020 at 15:33)</a>:</h4>
<p>Doesn't that mean that <code>T::add_assign(&amp;mut a, b)</code> is not the same as <code>a += b</code>?</p>



<a name="218217567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218217567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218217567">(Nov 29 2020 at 15:40)</a>:</h4>
<p>It should probably be <code>a</code> then <code>b</code> to match most other language constructs and the method call desugaring</p>



<a name="218218025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218218025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218218025">(Nov 29 2020 at 15:54)</a>:</h4>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<a name="218220851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218220851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218220851">(Nov 29 2020 at 17:14)</a>:</h4>
<p>I imagine the main place where users would notice a difference is something like <code>self.foo += self.foo</code> where the <code>a</code> then <code>b</code> order will cause a double borrow in the same way as often happens with <code>self.foo(self.bar())</code> where both <code>foo</code> and <code>bar</code> take <code>&amp;mut self</code></p>



<a name="218220938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218220938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218220938">(Nov 29 2020 at 17:17)</a>:</h4>
<p>I often wish that there were some special rule to make <code>self.foo(self.bar())</code> work, but if the receiver got evaluated last this would make method chains really weird</p>



<a name="218220995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218220995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218220995">(Nov 29 2020 at 17:18)</a>:</h4>
<p>I don't know how common <code>*self.foo() += 1</code> is but I guess that would hit this evaluation order issue</p>



<a name="218221481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218221481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218221481">(Nov 29 2020 at 17:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218217567">said</a>:</p>
<blockquote>
<p>It should probably be <code>a</code> then <code>b</code> to match most other language constructs and the method call desugaring</p>
</blockquote>
<p><code>=</code> is right-to-left, so I'd think <code>+=</code> should also be right-to-left. the desugaring can be changed.</p>



<a name="218243602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218243602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218243602">(Nov 30 2020 at 02:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218214547">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/218196592">said</a>:</p>
<blockquote>
<p>Anyways, my main question remains unanswered. And it's blocking me on the reference PR for evaluation order.</p>
</blockquote>
<p>I thought <a href="#narrow/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types/near/217833756">this is the answer</a>? We should probably add tests to confirm this.</p>
</blockquote>
<p>It's not an answer in that while it's the observed behavior for evaluation order, it's not a definition of what <code>a += b</code>. Specifically, I want to know if, where it's not primitive compound assignment, is it defined as a desugaring to <code>%OP%Assign::(&amp;mut a, b)</code> or if the semantics have more nuance?</p>



<a name="218243689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Semantics%20of%20%60a%20%2B%3D%20b%60%20for%20non-primitive%20types/near/218243689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Semantics.20of.20.60a.20.2B.3D.20b.60.20for.20non-primitive.20types.html#218243689">(Nov 30 2020 at 02:50)</a>:</h4>
<p>I linked to a test for the current behavior <a href="https://github.com/rust-lang/rust/blob/master/src/test/ui/expr/compound-assignment/eval-order.rs">earlier</a>. So the current behavior is at least tested.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>